﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MpSoft.Library.DTO;
using MpSoft.Library.BUSS.Categories;

namespace MpSoft.Web.AdminCP.ToolsBar
{
    public partial class Categories : UserControl
    {
        #region Var

        private int selectValue = 0;
        private Category category = null;

        #endregion

        #region Method

        private void BindControl()
        {
            if (ddlCategories.Items.Count < 1)
            {
                string module = string.IsNullOrEmpty(Module) ? null : Module,
                    menuid = string.IsNullOrEmpty(MenuID) ? null : MenuID;

                var categories = CategoryBUSS.Category_ByParentID(ParentID, module, menuid, 0);
                ddlCategories.Items.Add(new ListItem("-- Chọn danh mục --", ""));
                if (categories != null)
                {
                    var items = GetCategories("", module, menuid, categories).ToArray();
                    ddlCategories.Items.AddRange(items);
                }
            }

            if (ddlCategories.Items.FindByValue(selectValue.ToString()) != null)
                ddlCategories.Items.FindByValue(selectValue.ToString()).Selected = true;
        }

        private List<ListItem> GetCategories(string space, string module, string menuid, List<Category> categoryies)
        {
            List<ListItem> listCategory = new List<ListItem>();
            if (categoryies.Count > 0)
            {
                for (int i = 0; i < categoryies.Count; i++)
                {
                    var obj = categoryies[i];
                    listCategory.Add(new ListItem(space + obj.CategoryName, obj.CategoryID.ToString()));
                    var sub_categories = CategoryBUSS.Category_ByParentID(obj.CategoryID, module, menuid, 0);
                    if (sub_categories != null)
                        listCategory.AddRange(GetCategories("--|" + space, module, menuid, sub_categories));
                }
            }
            return listCategory;
        }

        #endregion

        #region Event 

        protected void Page_Load(object sender, EventArgs e)
        {
            BindControl();
        }

        #endregion

        #region Properties

        public string MenuID
        {
            set;
            get;
        }
        public string Module
        {
            set;
            get;
        }

        public int ParentID
        {
            set;
            get;
        }

        public int SelectVlaue
        {
            set {
                selectValue = value;
            }
            get {
                if (!string.IsNullOrEmpty(ddlCategories.SelectedValue))
                    return int.Parse(ddlCategories.SelectedValue);
                return -1;
            }
        }

        public int Level
        {
            get
            {
                if (!string.IsNullOrEmpty(ddlCategories.SelectedValue))
                {
                    category = CategoryBUSS.Category_GetByID(int.Parse(ddlCategories.SelectedValue));
                    if (category == null)
                        return 0;
                    return category.Level + 1;
                }
                    
                return 0;
            }
        }

        public Category Category
        {
            set { category = value; }
            get {
                if (!string.IsNullOrEmpty(ddlCategories.SelectedValue))
                    category = CategoryBUSS.Category_GetByID(int.Parse(ddlCategories.SelectedValue));
                return category;
            }
        }

        public DropDownList DdlCategories
        {
            set { ddlCategories = value; }
            get { return ddlCategories; }
        }

        #endregion
    }
}